<script setup>
import { ref, onMounted, onBeforeUnmount } from 'vue'

const searchText = ref('')

// ================== 神秘功能：搜索建议词 ==================
const suggestions = ref([
  '老人与海',
  '三国演义',
  '红楼梦',
  '科学快报',
  '英语周报',
  '数学教科书',
  '高等数学',
])
const showSuggestions = ref(false)
const filteredSuggestions = ref([])

const onInput = () => {
  if (searchText.value.trim() === '') {
    showSuggestions.value = false
    filteredSuggestions.value = []
    return
  }
  filteredSuggestions.value = suggestions.value.filter((item) => item.includes(searchText.value))
  showSuggestions.value = filteredSuggestions.value.length > 0
}
const onFocus = () => {
  if (searchText.value.trim() === '') {
    filteredSuggestions.value = suggestions.value.slice()
    showSuggestions.value = true
  } else {
    onInput()
  }
}
const onBlur = () => {
  setTimeout(() => {
    showSuggestions.value = false
  }, 100) // 延迟隐藏，便于点击
}
const selectSuggestion = (item) => {
  searchText.value = item
  showSuggestions.value = false
}

const carouselImages = [
  new URL('@/assets/images/picture01.jpg', import.meta.url).href,
  new URL('@/assets/images/picture02.jpg', import.meta.url).href,
  new URL('@/assets/images/picture03.jpg', import.meta.url).href,
]
const carouselIndex = ref(0)
let carouselTimer = null

const nextSlide = () => {
  carouselIndex.value = (carouselIndex.value + 1) % carouselImages.length
}

const startCarousel = () => {
  carouselTimer = setInterval(() => {
    nextSlide()
  }, 2000)
}
const stopCarousel = () => {
  if (carouselTimer) clearInterval(carouselTimer)
}

onMounted(() => {
  startCarousel()
})
onBeforeUnmount(() => {
  stopCarousel()
})

const categories = ['文学小说', '生活', '文艺', '人文社科', '科技', '少儿教育', '音响/期刊']

const bookImages = [
  new URL('@/assets/images/book-1.png', import.meta.url).href,
  new URL('@/assets/images/book-2.png', import.meta.url).href,
  new URL('@/assets/images/book-3.png', import.meta.url).href,
  new URL('@/assets/images/book-4.png', import.meta.url).href,
  new URL('@/assets/images/book-5.png', import.meta.url).href,
  new URL('@/assets/images/book-6.png', import.meta.url).href,
  new URL('@/assets/images/book-7.png', import.meta.url).href,
  new URL('@/assets/images/book-8.png', import.meta.url).href,
  new URL('@/assets/images/book-9.png', import.meta.url).href,
  new URL('@/assets/images/book-10.png', import.meta.url).href,
]

const hot_category_show = new URL('@/assets/images/home-hot-category-show.png', import.meta.url)
  .href
const phoneApp = new URL('@/assets/images/phone-app.png', import.meta.url).href
const returnTop = new URL('@/assets/images/return-top.png', import.meta.url).href

const scrollToTop = () => {
  window.scrollTo({
    top: 0,
    behavior: 'smooth',
  })
}

const search = () => {
  // 实现搜索功能
  console.log('搜索:', searchText.value)
}
</script>

<template>
  <div class="home-page">
    <!-- 使用全局顶部导航栏 -->

    <!-- ====== 首页搜索框 ====== -->
    <div class="home-page-banner">
      <form class="home-page-banner-search" @submit.prevent="search" autocomplete="off">
        <input
          type="text"
          placeholder="书名/作者/出版社"
          v-model="searchText"
          @input="onInput"
          @focus="onFocus"
          @blur="onBlur"
        />
        <button type="submit">搜索</button>
        <!-- ================== 神秘功能：搜索建议下拉 ================== -->
        <ul v-if="showSuggestions" class="home-page-search-suggestion-list">
          <li
            v-for="(item, idx) in filteredSuggestions"
            :key="idx"
            @mousedown.prevent="selectSuggestion(item)"
          >
            {{ item }}
          </li>
        </ul>
      </form>
    </div>
    <!-- ====== 主内容区：左侧分类+轮播图 ====== -->
    <div class="home-page-main-content">
      <div class="home-page-main-content-center">
        <!-- 分类导航 -->
        <div class="home-page-main-content-center-category-nav">
          <ul>
            <li v-for="(item, idx) in categories" :key="idx">
              <router-link to="/selective">{{ item }}</router-link>
            </li>
          </ul>
        </div>
        <!-- 轮播图 -->
        <div class="home-page-main-content-center-banner">
          <el-carousel
            :pause-on-hover="true"
            indicator-position="outside"
            motion-blur
            :autoplay="false"
            style="height: 100%"
          >
            <el-carousel-item v-for="(item, index) in carouselImages" :key="index">
              <img :src="item" alt="" />
            </el-carousel-item>
          </el-carousel>
        </div>
      </div>
    </div>

    <!--热门书单-->
    <div class="home-page-hot-list">
      <div class="home-page-hot-list-book">
        <div class="home-page-hot-list-book-header">
          <h2 class="home-page-hot-list-book-title">最新热门书单</h2>
          <router-link to="/more" class="home-page-hot-list-book-more">查看更多</router-link>
        </div>
        <div class="home-page-hot-list-book-show">
          <div class="home-page-hot-list-book-show-item" v-for="(img, n) in bookImages" :key="n">
            <router-link to="/commodity-detail" style="text-decoration: none">
              <img :src="img" alt="" />
              <div class="home-page-hot-list-book-show-item-info">
                <div class="home-page-hot-list-book-show-item-info-title">测试样例-书本</div>
                <div class="home-page-hot-list-book-show-item-info-desc">测试样例-书本介绍</div>
                <div class="home-page-hot-list-book-show-item-info-price">￥19.99</div>
              </div>
            </router-link>
          </div>
        </div>
      </div>
    </div>

    <!--热门图书类别-->
    <!--类别一-->
    <div class="home-page-hot-category">
      <div class="home-page-hot-category-title">
        <div class="home-page-hot-category-title-main">科技</div>
        <router-link to="/more" class="home-page-hot-category-title-link">查看更多</router-link>
      </div>
      <div class="home-page-hot-category-largedisplay">
        <div class="home-page-hot-category-largedisplay-left">
          <div class="home-page-hot-category-largedisplay-left-title">测试样例-书本标题</div>
          <div class="home-page-hot-category-largedisplay-left-desc">测试样例-书本介绍</div>
          <div class="home-page-hot-category-largedisplay-left-price">￥19.99</div>
          <button class="home-page-hot-category-largedisplay-left-button">加入购物车</button>
        </div>
        <div class="home-page-hot-category-largedisplay-right">
          <img :src="hot_category_show" alt="" />
        </div>
      </div>
      <div class="home-page-hot-category-display">
        <div
          class="home-page-hot-category-display-item"
          v-for="(img, n) in bookImages.slice(0, 4)"
          :key="n"
        >
          <router-link to="/commoditydetail" style="text-decoration: none">
            <img :src="img" alt="" />
            <div class="home-page-hot-category-display-item-info">
              <div class="home-page-hot-category-display-item-info-title">测试样例-书本</div>
              <div class="home-page-hot-category-display-item-info-desc">测试样例-书本介绍</div>
              <div class="home-page-hot-category-display-item-info-price">￥19.99</div>
            </div>
          </router-link>
        </div>
      </div>

      <!--类别二-->
      <div class="home-page-hot-category-title">
        <div class="home-page-hot-category-title-main">名人传记</div>
        <router-link to="/more" class="home-page-hot-category-title-link">查看更多</router-link>
      </div>
      <div class="home-page-hot-category-largedisplay">
        <div class="home-page-hot-category-largedisplay-left">
          <div class="home-page-hot-category-largedisplay-left-title">测试样例-书本标题</div>
          <div class="home-page-hot-category-largedisplay-left-desc">测试样例-书本介绍</div>
          <div class="home-page-hot-category-largedisplay-left-price">￥19.99</div>
          <button class="home-page-hot-category-largedisplay-left-button">加入购物车</button>
        </div>
        <div class="home-page-hot-category-largedisplay-right">
          <img :src="hot_category_show" alt="" />
        </div>
      </div>
      <div class="home-page-hot-category-display">
        <div
          class="home-page-hot-category-display-item"
          v-for="(img, n) in bookImages.slice(5, 9)"
          :key="n"
        >
          <router-link to="/commoditydetail" style="text-decoration: none">
            <img :src="img" alt="" />
            <div class="home-page-hot-category-display-item-info">
              <div class="home-page-hot-category-display-item-info-title">测试样例-书本</div>
              <div class="home-page-hot-category-display-item-info-desc">测试样例-书本介绍</div>
              <div class="home-page-hot-category-display-item-info-price">￥19.99</div>
            </div>
          </router-link>
        </div>
      </div>

      <!--类别三-->
      <div class="home-page-hot-category-title">
        <div class="home-page-hot-category-title-main">小说</div>
        <router-link to="/more" class="home-page-hot-category-title-link">查看更多</router-link>
      </div>
      <div class="home-page-hot-category-display">
        <div
          class="home-page-hot-category-display-item"
          v-for="(img, n) in bookImages.slice(2, 6)"
          :key="n"
        >
          <router-link to="/commoditydetail" style="text-decoration: none">
            <img :src="img" alt="" />
            <div class="home-page-hot-category-display-item-info">
              <div class="home-page-hot-category-display-item-info-title">测试样例-书本</div>
              <div class="home-page-hot-category-display-item-info-desc">测试样例-书本介绍</div>
              <div class="home-page-hot-category-display-item-info-price">￥19.99</div>
            </div>
          </router-link>
        </div>
      </div>

      <!--类别四-->
      <div class="home-page-hot-category-title">
        <div class="home-page-hot-category-title-main">少儿教育</div>
        <router-link to="/more" class="home-page-hot-category-title-link">查看更多</router-link>
      </div>
      <div class="home-page-hot-category-display">
        <div
          class="home-page-hot-category-display-item"
          v-for="(img, n) in bookImages.slice(6, 10)"
          :key="n"
        >
          <router-link to="/commoditydetail" style="text-decoration: none">
            <img :src="img" alt="" />
            <div class="home-page-hot-category-display-item-info">
              <div class="home-page-hot-category-display-item-info-title">测试样例-书本</div>
              <div class="home-page-hot-category-display-item-info-desc">测试样例-书本介绍</div>
              <div class="home-page-hot-category-display-item-info-price">￥19.99</div>
            </div>
          </router-link>
        </div>
      </div>
    </div>

    <div class="home-page-blank"></div>

    <!-- ====== 右侧固定栏 ====== -->
    <div class="home-page-fixed-bar">
      <div class="home-page-fixed-bar-item">
        <img :src="phoneApp" alt="" />
        <div class="home-page-fixed-bar-item-text">
          <router-link to="/download">下载App</router-link>
        </div>
      </div>
      <div class="home-page-fixed-bar-item">
        <img :src="returnTop" alt="" />
        <div class="home-page-fixed-bar-item-text">
          <a href="#" @click.prevent="scrollToTop">返回顶部</a>
        </div>
      </div>
    </div>
    <!-- ====== 使用全局页脚 -->
  </div>
</template>

<style lang="scss" scoped>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  text-decoration: none;
  list-style: none;
}

.home-page {
  width: 98vw;
  margin: 0;
  padding: 0;
  background: #f7f7f7;
  overflow-x: hidden;
  /* 防止多余滚动条 */
}

.home-page-banner {
  position: relative;
  .home-page-banner-search {
    width: 100%;
    background: #f7f7f7;
    padding: 30px 20px 10px 0;
    display: flex;
    justify-content: center;
    position: relative;
    input {
      width: 700px;
      height: 48px;
      border-radius: 10px 0 0 10px;
      border: 1px solid #e0e0e0;
      outline: none;
      font-size: 18px;
      font-weight: 550;
      padding-left: 20px;
      color: #444;
      background: #fafafa;
    }
    button {
      width: 100px;
      height: 48px;
      border-radius: 0 10px 10px 0;
      background: #222;
      border: none;
      color: #fff;
      font-size: 18px;
      font-weight: 550;
      cursor: pointer;
      transition: background 0.2s;
      &:hover {
        background: #ff9800;
      }
    }
    /* ================== 神秘功能：搜索建议下拉样式 ================== */
    .home-page-search-suggestion-list {
      position: absolute;
      left: 23%;
      top: 80px;
      width: 700px;
      background: #fff;
      border-top: none;
      z-index: 10;
      max-height: 300px;
      overflow-y: auto;
      box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
      li {
        padding: 8px 16px;
        cursor: pointer;
        color: #222;
        font-size: 16px;
        font-weight: 550;
        &:hover {
          background: #f7f7f7;
          color: #ff9800;
        }
      }
    }
  }
}

.home-page-main-content {
  width: 100%;
  margin: 0 auto;
  display: flex;
  justify-content: center;
  box-sizing: border-box;
  .home-page-main-content-center {
    display: flex;
    flex-direction: row;
    width: 100%;

    height: 500px;
    background: white;
    margin: 20px auto;
    .home-page-main-content-center-category-nav {
      width: 110px;
      background: #222;
      display: flex;
      flex-direction: column;
      justify-content: center;
      padding: 10px 0;
      cursor: pointer;
      a {
        height: 48px;
        line-height: 48px;
        padding-left: 18px;
        font-size: 17px;
        font-weight: 550;
        transition: background 0.2s;
        cursor: pointer;
        color: #fff;
      }
      li:hover {
        background: #ff9800;
      }
    }
    .home-page-main-content-center-banner {
      width: 100%;
      height: 100%;
      :deep(.el-carousel__container) {
        height: 100%;
      }
      img {
        width: 100%;
        height: 100%;
        object-fit: cover;
        display: block;
      }
    }
  }
}

.home-page-hot-list {
  width: 100%;
  max-width: 1200px;
  margin: 20px auto;
  display: flex;
  justify-content: center;
  .home-page-hot-list-book {
    max-width: 1200px;
    width: 100%;
    margin: 0 auto 18px auto;
    .home-page-hot-list-book-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 18px;
    }
    .home-page-hot-list-book-title {
      font-size: 24px;
      font-weight: 600;
      color: #222;
    }
    .home-page-hot-list-book-more {
      font-size: 15px;
      color: #222;
      background: #f7f7f7;
      font-weight: 550;
      margin-left: 24px;
      transition: color 0.2s;
      &:hover {
        color: #ff9800;
      }
    }
    .home-page-hot-list-book-show {
      width: 100%;
      display: flex;
      flex-wrap: wrap;
      gap: 24px 18px;
      justify-content: flex-start;
      justify-content: space-between;
      .home-page-hot-list-book-show-item {
        width: 225px;
        height: 320px;
        border-radius: 12px;
        margin-bottom: 10px;
        transition: all 0.3s linear;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: flex-start;
        cursor: pointer;
        background: #fff;
        &:hover {
          transform: translateY(-1px) scale(1);
          box-shadow: 0 8px 24px rgba(0, 0, 0, 0.13);
          background: #fff;
        }
        img {
          width: 150px;
          height: 180px;
          object-fit: cover;
          border-radius: 8px;
          margin-top: 15px;
          background: #fff;
          &:hover {
            background: #fff;
          }
        }
        .home-page-hot-list-book-show-item-info {
          width: 100%;
          padding: 10px 0 0 0;
          display: flex;
          flex-direction: column;
          align-items: flex-start;
          .home-page-hot-list-book-show-item-info-title {
            font-size: 18px;
            font-weight: 550;
            color: #222;
          }
          .home-page-hot-list-book-show-item-info-desc {
            font-size: 14px;
            font-weight: 500;
            color: #888;
            margin-bottom: 25px;
          }
          .home-page-hot-list-book-show-item-info-price {
            font-size: 14px;
            font-weight: bold;
            color: #222;
          }
        }
      }
    }
  }
}

.home-page-hot-category {
  width: 100%;
  max-width: 1200px;
  margin: 0px auto;
  display: flex;
  justify-content: center;
  flex-direction: column;
  .home-page-hot-category-title {
    margin-top: 50px;
    display: flex;
    justify-content: space-between;
    .home-page-hot-category-title-main {
      font-size: 24px;
      font-weight: 600;
      color: #222;
    }
    .home-page-hot-category-title-link {
      font-size: 15px;
      color: #222;
      background: #f7f7f7;
      font-weight: 550;
      margin-left: 24px;
      transition: color 0.2s;
      &:hover {
        color: #ff9800;
      }
    }
  }
  .home-page-hot-category-largedisplay {
    cursor: pointer;
    margin-top: 20px;
    width: 100%;
    height: 400px;
    background: #fff;
    border-radius: 20px;
    display: flex;
    .home-page-hot-category-largedisplay-left {
      padding: 80px;
      .home-page-hot-category-largedisplay-left-title {
        font-size: 40px;
        font-family: 'Courier New', Courier, monospace;
      }
      .home-page-hot-category-largedisplay-left-desc {
        font-size: 25px;
        color: #888;
      }
      .home-page-hot-category-largedisplay-left-price {
        margin-top: 100px;
        font-size: 20px;
        font-weight: 550;
      }
      .home-page-hot-category-largedisplay-left-button {
        margin-top: 20px;
        width: 150px;
        height: 40px;
        background: #222;
        color: #fff;
        font-size: 20px;
        border-radius: 20px;
        border: #222;
        font-family: 'Courier New', Courier, monospace;
        font-weight: 550;
        &:hover {
          background: #ff9800;
          transition: background 0.2s;
          cursor: pointer;
        }
      }
    }
    .home-page-hot-category-largedisplay-right {
      margin: 30px auto;
      img {
        width: 350px;
        height: 350px;
      }
    }
    &:hover {
      box-shadow: 0 8px 24px rgba(0, 0, 0, 0.13);
      transition: 0.3s;
      background: #fff;
    }
  }
  .home-page-hot-category-display {
    display: flex;
    margin-top: 20px;
    width: 100%;
    height: 400px;
    justify-content: space-between;
    .home-page-hot-category-display-item {
      cursor: pointer;
      width: 285px;
      height: 400px;
      background: #fff;
      border-radius: 15px;
      img {
        width: 180px;
        height: 300;
        margin: 20px 50px 0 50px;
      }
      &:hover {
        box-shadow: 0 8px 24px rgba(0, 0, 0, 0.13);
        transition: 0.3s;
        background: #fff;
      }
      .home-page-hot-category-display-item-info {
        margin: 10px 0 0 30px;

        .home-page-hot-category-display-item-info-title {
          font-size: 18px;
          font-weight: 550;
          color: #222;
        }
        .home-page-hot-category-display-item-info-desc {
          font-size: 14px;
          font-weight: 500;
          color: #888;
          margin-bottom: 25px;
        }
        .home-page-hot-category-display-item-info-price {
          font-size: 14px;
          font-weight: bold;
          color: #222;
        }
      }
    }
  }
}

.home-page-fixed-bar {
  width: 80px;
  min-height: 150px;
  position: fixed;
  right: 20px;
  top: 450px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.13);
  background: #fff;
  border-radius: 16px;
  text-align: center;
  z-index: 100;
  padding: 10px 0;
  display: flex;
  flex-direction: column;
  gap: 12px;
  .home-page-fixed-bar-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    margin-bottom: 10px;
    img {
      width: 36px;
      height: 36px;
      margin-bottom: 10px;
    }
    .home-page-fixed-bar-item-text {
      a {
        text-decoration: none;
        color: #222;
        font-size: 17px;
        font-weight: 550;
        line-height: 18px;
        word-break: keep-all;
        font-family: 'Courier New', Courier, monospace;
        &:hover {
          color: #ff9800;
          background: #fff;
        }
      }
    }
  }
}

.el-carousel__container {
  height: 400px;
  opacity: 0.75;
  margin: 0;
  text-align: center;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}

.el-carousel__item img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
}
</style>
